package com.example.waibao.controller;


//import com.example.waibao.Component.LoginUserHandlerMethodArgumentResolver;
import com.example.waibao.Component.UserHelper;
import com.example.waibao.entity.*;
import com.example.waibao.service.UserService;
import com.example.waibao.utils.Jwtutil;
import com.example.waibao.utils.RedisCache;
import com.github.pagehelper.PageInfo;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.xml.soap.MimeHeaders;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class userController {

    @Autowired
    private UserService userService;

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private UserHelper userHelper;

//    @Autowired
//    private LoginUserHandlerMethodArgumentResolver loginUserHandlerMethodArgumentResolver;

    //解析token返回token携带信息

    @GetMapping("get_myself")
    public ResponseResult solve_token() throws Exception {

//        loginUserHandlerMethodArgumentResolver.resolveArgument();

        User user = userHelper.getCurrentUser();
//        System.out.println(token);
//        Claims claims = Jwtutil.parseJWT(token);
//        String userid = claims.getSubject();
//        //从redis中获取用户信息
//        String redisKey = "login:" + userid;
//        //反序列化
//        LoginUser loginUser = redisCache.getCacheObject(redisKey);

        return new ResponseResult(200,"获取个人用户信息成功", user);
    }


    //审核教师的注册，并分配职位，设置权限

    //教师注册
    @PostMapping("teacher_register")
    public ResponseResult teacher_register(@RequestBody User user){

        boolean b = userService.check_username_reappear(user.getUser_name(),user.getSchool_id());


        if(!b){
            return new ResponseResult(400,"用户名已存在",user);
        }

        String password = user.getPassword();

       // userService.add_teacher(user);
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        System.out.println(user.getPassword());
        user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
        userService.add_teacher(user);

        user.setPassword(password);
        return new ResponseResult(200,"注册成功请耐心等待验证通过",user);
    }

    //获取所有用户信息
    @GetMapping("get_users")
    public ResponseResult get_users(int status,
                                    @RequestParam(name = "nick_name", defaultValue = "0")String nick_name,
                                    @RequestParam(name = "page_num", defaultValue = "1")Integer page_num,
                                    @RequestParam(name = "page_size", defaultValue = "10")Integer page_size){

        if(nick_name.equals("0")){
            nick_name = "全部";
        }
        User user = userHelper.getCurrentUser();
        int school_id = user.getSchool_id();
      //  List<User> users = userService.get_users(status,school_id);
        PageInfo<User> users = userService.get_users(page_num, page_size,status,school_id,nick_name);

        return new ResponseResult(200,"获取用户信息成功",users);

    }

    //审核教师的注册信息
    @PostMapping("solve_users")
    public ResponseResult solve_users(@RequestBody User user){

        if(user.getStatus() == 1){
            userService.solve_users(user.getId());
        }else if(user.getStatus() == 0){
            userService.delete_users(user.getId());
        }

        return new ResponseResult(200,"审核注册用户成功成功");
    }

    //设置权限
    @PostMapping("set_sys_authority")
    public ResponseResult set_sys_authority(@RequestBody Map<String,Integer> role){

        int user_id = role.get("user_id");
        int role_id = role.get("role_id");

        userService.set_sys_authority(user_id,role_id);
        userService.update_user_type(user_id,role_id);

        return new ResponseResult(200,"设置权限成功");
    }


    //获取权限
    @GetMapping("get_all_authority")
    public ResponseResult get_all_authority(){

        List<authority> perms = userService.get_all_authority();

        return new ResponseResult(200,"获取权限成功",perms);
    }



    //获取所有已注册的学校信息
    @GetMapping("get_school")
    public ResponseResult get_school(){

        List<school> schools = userService.get_school();
        return new ResponseResult(200,"获取学校信息成功",schools);
    }

    //设置头像


    //学校注册
    @PostMapping("school_register")
    public ResponseResult school_register(@RequestBody school school){

        boolean b = userService.check_school_reappear(school.getName());


        if(!b){
            return new ResponseResult(400,"该学校已注册",school.getName());
        }

        school school1 = new school(0,school.getName(),0);
        userService.school_register(school1);

        //初始admin用户下发等待软件商验证通过
        User user = new User();
        user.setUser_name("admin");
        user.setSchool_id(school1.getId());
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        user.setPassword(bCryptPasswordEncoder.encode("123456"));
        user.setNick_name("超级管理员");
        userService.add_a_admin(user);

        userService.set_authority(user.getId());


        return new ResponseResult(200,"注册学校成功",school.getName());
    }

    //学校专业设置
    @PostMapping("set_speciality")
    public ResponseResult set_speciality(@RequestBody speciality speciality){

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
        java.util.Date day=new Date();
        String d = format.format(day);
        Date date1 = null;
        try {
            date1 = format.parse(d);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        User user = userHelper.getCurrentUser();

        int school_id = user.getSchool_id();

//        List<speciality> specialities = userService.get_speciality(school_id);
//        if(!specialities.isEmpty()){
//            userService.delete_speciality(school_id);
//            userService.delete_speciality_rank(school_id);
//        }

        speciality.setSchool_id(school_id);

        for (String s : speciality.getType() ) {
          // userService.set_speciality(speciality.getSchool_id(), s);
            a_speciality a_speciality = new a_speciality(0,s,speciality.getSchool_id());
            userService.set_speciality(a_speciality);
            //获取专业表id主键自增的值
            System.out.println(a_speciality.getId());
            //设置专业排行榜初始值
            userService.set_speciality_rank(a_speciality.getId(),date1,speciality.getSchool_id());
        }

        return new ResponseResult(200,"专业设置成功");
    }

    @PostMapping("delete_speciality")
    public ResponseResult delete_speciality(@RequestBody a_speciality speciality){

        userService.delete_speciality(speciality.getId());
        userService.delete_speciality_rank(speciality.getId());

        return new ResponseResult(200,"专业删除成功");
    }

    @PostMapping("update_speciality")
    public ResponseResult update_speciality(@RequestBody a_speciality speciality){


        userService.update_speciality(speciality);


        return new ResponseResult(200,"专业更新成功");
    }


    @GetMapping("get_speciality")
    public ResponseResult get_speciality(){
        User user = userHelper.getCurrentUser();
        int school_id = user.getSchool_id();
        List<a_speciality> specialities = userService.get_speciality(school_id);
        return new ResponseResult(200,"获取学校专业信息成功", specialities);
    }



    //确认密码
    @PostMapping("check_password")
    public ResponseResult check_password(@RequestBody User user){
        User user2 = userHelper.getCurrentUser();
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        System.out.println(user.getPassword());

     //   user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
      //  String p2 = userService.check_password(user);
        //System.out.println(bCryptPasswordEncoder.matches(user.getPassword()));
        HashMap map = new HashMap();
        map.put("password",user.getPassword());

        if(bCryptPasswordEncoder.matches(user.getPassword(),user2.getPassword())){
            user2.setPassword(user.getPassword());
            return  new ResponseResult(200,"确认密码成功",user2);
        }else{
            return  new ResponseResult(400,"确认密码失败",user);
        }


    }

    //修改个人信息
    @PostMapping("update_a_user")
    public ResponseResult update_a_user(@RequestBody User user){
        User user2 = userHelper.getCurrentUser();
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();

        boolean b = userService.check_username_reappear(user.getUser_name(),user2.getSchool_id());

        user.setId(user2.getId());
        user.setUser_type(user2.getUser_type());
        user.setSchool_id(user2.getSchool_id());
        user.setStatus(user2.getStatus());

        String password = user.getPassword();

        if(!b && !(user.getUser_name().equals(user2.getUser_name()))){
            return  new ResponseResult(400,"用户名已存在",user);
        }

        user.setId(user2.getId());

        System.out.println(user.getPassword());
        user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));

        userService.update_a_user(user);

        user.setPassword(password);


        return  new ResponseResult(200,"修改个人信息成功",user);
    }

}
